package me.fung.wubi.repo

import mu.KotlinLogging
import org.ktorm.database.Database
import java.sql.Connection

const val DATA = ".data"
const val TAG = ".data/init"

private val logger = KotlinLogging.logger {}
val database = Database.connect("jdbc:sqlite:$DATA/database", driver = "org.sqlite.JDBC", user = "", password = "")

fun createDatabase() {
    database.useTransaction {
        database.useConnection { connection: Connection ->
            createTables(connection)
        }
    }
    logger.info { "create database success" }
}

private fun Connection.sql(sql: String) {
    this.createStatement().execute(sql)
}

private fun createTables(connection: Connection) {
    // DBConfig ============================================================
    var sql = """
        create table DBConfig(
        name varchar(255) primary key,
        value varchar(255),
        info varchar(255),
        type integer(2)
        )
    """.trimIndent()
    connection.sql(sql)


}
